import asset from "@ohos.security.asset";
import util from "@ohos.util";
import hilog from "@ohos.hilog";
export class AssetStore {
    static async set(key, value, e10 = true) {
        let attr = new Map();
        if (canIUse("SystemCapability.Security.Asset")) {
            attr.set(asset.Tag.ALIAS, AssetStore.stringToArray(key));
            attr.set(asset.Tag.SECRET, AssetStore.stringToArray(value));
            attr.set(asset.Tag.SYNC_TYPE, asset.SyncType.THIS_DEVICE);
            attr.set(asset.Tag.CONFLICT_RESOLUTION, asset.ConflictResolution.THROW_ERROR);
            if (e10) {
                attr.set(asset.Tag.IS_PERSISTENT, e10);
            }
        }
        let result;
        if ((await AssetStore.has(key)).isSuccess) {
            result = await AssetStore.updateAssetMap(attr, attr);
        }
        else {
            result = await AssetStore.setAssetMap(attr);
        }
        if (result.isSuccess) {
            hilog.debug(0x1111, 'AssetStore', `AssetStore: Asset add succeeded. Key is ${key}, value is ${value}, isPersistent is ${e10}`);
            AppStorage.setOrCreate(key, value);
        }
        return result;
    }
    static async setAssetMap(attr) {
        try {
            if (canIUse("SystemCapability.Security.Asset")) {
                await asset.add(attr);
                return { isSuccess: true };
            }
            return { isSuccess: false, error: AssetStore.getUnSupportedPlatforms() };
        }
        catch (error) {
            const err = error;
            hilog.debug(0x1111, 'AssetStore', `AssetStore: Failed to add Asset. Code is ${err.code}, message is ${err.message}`);
            return { isSuccess: false, error: err };
        }
    }
    static async remove(key) {
        let query = new Map();
        if (canIUse("SystemCapability.Security.Asset")) {
            query.set(asset.Tag.ALIAS, AssetStore.stringToArray(key));
        }
        const result = await AssetStore.removeAssetMap(query);
        if (result.isSuccess) {
            hilog.debug(0x1111, 'AssetStore', `AssetStore: Asset remove succeeded. Key is ${key}`);
            AppStorage.setOrCreate(key, '');
        }
        return result;
    }
    static async removeAssetMap(attr) {
        try {
            if (canIUse("SystemCapability.Security.Asset")) {
                await asset.remove(attr);
                return { isSuccess: true };
            }
            return { isSuccess: false };
        }
        catch (error) {
            const err = error;
            hilog.debug(0x1111, 'AssetStore', `AssetStore: Failed to remove Asset. Code is ${err.code}, message is ${err.message}`);
            return { isSuccess: false, error: err };
        }
    }
    static async has(key) {
        if (canIUse("SystemCapability.Security.Asset")) {
            let query = new Map();
            query.set(asset.Tag.ALIAS, AssetStore.stringToArray(key));
            query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL);
            const result = await AssetStore.getAssetMap(query);
            const d10 = result.res;
            if (!d10) {
                return { isSuccess: false, error: result.error };
            }
            if (d10.length < 1) {
                return { isSuccess: false };
            }
        }
        return { isSuccess: false };
    }
    static async get(key) {
        if (canIUse("SystemCapability.Security.Asset")) {
            let query = new Map();
            query.set(asset.Tag.ALIAS, AssetStore.stringToArray(key));
            query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL);
            const result = await AssetStore.getAssetMap(query);
            const a10 = result.res;
            if (!a10) {
                return { isSuccess: false, error: result.error };
            }
            if (a10.length < 1) {
                return { isSuccess: false };
            }
            let b10 = a10[0].get(asset.Tag.SECRET);
            let c10 = AssetStore.arrayToString(b10);
            return { isSuccess: true, data: c10 };
        }
        return { isSuccess: false, data: "" };
    }
    static async getAssetMap(query) {
        try {
            if (canIUse("SystemCapability.Security.Asset")) {
                const z9 = await asset.query(query);
                return { res: z9 };
            }
            return { error: AssetStore.getUnSupportedPlatforms() };
        }
        catch (error) {
            const err = error;
            hilog.debug(0x1111, 'AssetStore', `AssetStore>getAssetMap: Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
            return { error: err };
        }
    }
    static async updateAssetMap(query, y9) {
        try {
            if (canIUse("SystemCapability.Security.Asset")) {
                await asset.update(query, y9);
                return { isSuccess: true };
            }
            return { isSuccess: false, error: AssetStore.getUnSupportedPlatforms() };
        }
        catch (error) {
            const err = error;
            hilog.debug(0x1111, 'AssetStore', `AssetStore: Failed to update Asset. Code is ${err.code}, message is ${err.message}`);
            return { isSuccess: false, error: err };
        }
    }
    static stringToArray(str) {
        let x9 = new util.TextEncoder();
        return x9.encodeInto(str);
    }
    static arrayToString(arr) {
        let w9 = util.TextDecoder.create('utf-8', { ignoreBOM: true });
        let str = w9.decodeWithStream(arr, { stream: false });
        return str;
    }
    static getUnSupportedPlatforms() {
        return { name: "AssetStore", message: "不支持该平台" };
    }
}
